﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：机构管理
 */

var acceptClick;
var keyValue = request("keyValue");
var schemeAuthorizes = []; // 模板权限人员
var authorizeType = 1;// 模板权限类型
var fileType = request("fileType");

var bootstrap = function ($, learun) {
    "use strict";

    function isRepeat(id) {
        var res = false;
        for (var i = 0, l = schemeAuthorizes.length; i < l; i++) {
            if (schemeAuthorizes[i].objectId == id) {
                learun.alert.warning('重复添加信息');
                res = true;
                break;
            }
        }
        return res;
    }

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            // 权限设置
            $('[name="authorizeType"]').on('click', function () {
                var $this = $(this);
                var value = $this.val();
                authorizeType = value;
                if (value == '1') {
                    $('#shcemeAuthorizeBg').show();
                } else {
                    $('#shcemeAuthorizeBg').hide();
                }
            });
            $('#authorize_girdtable').jfGrid({
                headData: [
                    {
                        label: "类型", name: "objectType", width: 80, align: "center",
                        formatter: function (cellvalue) {//审核者类型1.岗位2.角色3.用户
                            switch (parseInt(cellvalue)) {
                                case 1:
                                    return '岗位';
                                    break;
                                case 2:
                                    return '角色';
                                    break;
                                case 3:
                                    return '用户';
                                    break;
                            }
                        }
                    },
                    {label: "名称", name: "objectName", width: 120, align: "left"},
                    {
                        label: "权限", name: "authType", width: 200, align: "left",
                        edit: {
                            type: 'checkbox',
                            init: function (data, $edit) {// 在点击单元格的时候触发，可以用来初始化输入控件，行数据和控件对象

                            },
                            change: function (data, num) {// 行数据和行号

                            },
                            data: [
                                {'id': '1', 'text': '查看'},
                                {'id': '3', 'text': '下载'},
                                {'id': '4', 'text': '删除'},
                                {'id': '5', 'text': '复原'},
                                {'id': '6', 'text': '彻底删除'}
                            ],
                            dfvalue: '1,3'// 默认选中项
                        }
                    },
                    {
                        label: "过期时间", name: "expireTime", width: 100, align: "left",
                        edit: {
                            type: 'datatime',
                            dateformat: '0',       // 0:yyyy-MM-dd;1:yyyy-MM-dd HH:mm,格式
                            init: function (data, $edit) {// 在点击单元格的时候触发，可以用来初始化输入控件，行数据和控件对象

                            },
                            change: function (data, num) {// 行数据和行号

                            }
                        }
                    },
                ],
                isEdit: true
            });
            // 岗位添加
            $('#lr_post_authorize').on('click', function () {
                learun.layerForm({
                    id: 'AuthorizePostForm',
                    title: '添加岗位',
                    url: config.webportal_server + 'pages/workflow/wfscheme/postForm.html?flag=1&access_token=' + config.getToken().access_token,
                    width: 400,
                    height: 300,
                    callBack: function (id) {
                        return top[id].acceptClick(function (data) {
                            if (!isRepeat(data.auditorId)) {
                                var _data = {};
                                _data.id = learun.newGuid();
                                _data.objectName = data.auditorName;
                                _data.objectId = data.auditorId;
                                _data.objectType = data.type;
                                schemeAuthorizes.push(_data);
                                $('#authorize_girdtable').jfGridSet('refreshdata', {rowdatas: schemeAuthorizes});
                            }
                        });
                    }
                });
            });
            // 角色添加
            $('#lr_role_authorize').on('click', function () {
                learun.layerForm({
                    id: 'AuthorizeRoleForm',
                    title: '添加角色',
                    url: config.webportal_server + 'pages/workflow/wfscheme/roleForm.html?flag=1&access_token=' + config.getToken().access_token,
                    width: 400,
                    height: 300,
                    callBack: function (id) {
                        return top[id].acceptClick(function (data) {
                            if (!isRepeat(data.auditorId)) {
                                var _data = {};
                                _data.id = learun.newGuid();
                                _data.objectName = data.auditorName;
                                _data.objectId = data.auditorId;
                                _data.objectType = data.type;
                                schemeAuthorizes.push(_data);
                                $('#authorize_girdtable').jfGridSet('refreshdata', {rowdatas: schemeAuthorizes});
                            }
                        });
                    }
                });
            });
            // 人员添加
            $('#lr_user_authorize').on('click', function () {
                learun.layerForm({
                    id: 'AuthorizeUserForm',
                    title: '添加人员',
                    url: config.webportal_server + 'pages/workflow/wfscheme/userForm.html?flag=1&access_token=' + config.getToken().access_token,
                    width: 400,
                    height: 300,
                    callBack: function (id) {
                        return top[id].acceptClick(function (data) {
                            if (!isRepeat(data.auditorId)) {
                                var _data = {};
                                _data.id = learun.newGuid();
                                _data.objectName = data.auditorName;
                                _data.objectId = data.auditorId;
                                _data.objectType = data.type;
                                schemeAuthorizes.push(_data);
                                $('#authorize_girdtable').jfGridSet('refreshdata', {rowdatas: schemeAuthorizes});
                            }
                        });
                    }
                });
            });
            // 人员移除
            $('#lr_delete_authorize').on('click', function () {
                var _id = $('#authorize_girdtable').jfGridValue('id');
                if (learun.checkrow(_id)) {
                    learun.layerConfirm('是否确认删除该项！', function (res, index) {
                        if (res) {
                            for (var i = 0, l = schemeAuthorizes.length; i < l; i++) {
                                if (schemeAuthorizes[i].id == _id) {
                                    schemeAuthorizes.splice(i, 1);
                                    $('#authorize_girdtable').jfGridSet('refreshdata', {rowdatas: schemeAuthorizes});
                                    break;
                                }
                            }
                            top.layer.close(index); //再执行关闭
                        }
                    });
                }
            });
        },
        initData: function () {
            $.lrSetForm(config.base_server() + 'fileinfos/share/getPermission?keyValue=' + keyValue, function (data) {
                if (data.length > 0 && data[0].objectType != '4') {
                    $('#authorizeType2').trigger('click');
                    schemeAuthorizes = data;
                    $('#authorize_girdtable').jfGridSet('refreshdata', {rowdatas: schemeAuthorizes});
                    authorizeType = 2;
                }
            });
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (authorizeType == 1) {
            schemeAuthorizes = [];
            schemeAuthorizes.push({
                id: learun.newGuid(),
                objectType: '4'
            });
        }
        var postData = schemeAuthorizes;

        $.lrSaveForm(config.base_server() + 'fileinfos/share/' + keyValue + '?isShare=1&fileType=' + fileType + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };
    page.init();
}